from common.base.base_handler import BaseHandler
from services.relays_module.model import RelaysModule
from services.relays_module.schemas.configs import RmConfig
from common.third_party.save_config import save_config
from settings import settings


class RelaysModuleHandler(BaseHandler):
    __slots__ = ['model']

    async def handle_set_configure(self, params: dict) -> bool:
        index, config = params['index'], RmConfig(**params['config'])
        self.model.rm_configs[index] = config
        save_config(f'rm/rm_config_{index}', self.model.rm_configs[index])

        return True

    async def handle_get_configure(self, params: dict) -> dict:
        index = params['index']
        return self.model.rm_configs[index]

    async def handle_get_telemetry(self, params: dict) -> dict:
        index = params['index']
        return self.model.rm_telemetries[index]

    def __init__(self) -> None:
        self.model = RelaysModule()

        super(RelaysModuleHandler, self).__init__(request_queue_uuid=settings.RELAYS_MODULE_REQUEST_QUEUE)

        self.handle('set_configure', self.handle_set_configure)
        self.handle('get_configure', self.handle_get_configure)
        self.handle('get_telemetry', self.handle_get_telemetry)
